package bancosys.tec.persist.persister.listener;

import java.lang.reflect.TypeVariable;

import jmine.tec.annotations.Unmodifiable;
import jmine.tec.persist.PersistMessages;
import jmine.tec.utils.reflection.GenericTypeResolverImpl;
import bancosys.tec.persist.exception.PersistenceException;

/**
 * Implementacao abstrata de {@link PurePersisterListener}
 * 
 * @author takeshi
 * @param <T> o tipo da entidade
 */
@Unmodifiable
public abstract class AbstractPurePersisterListener<T> implements PurePersisterListener<T> {

    private final Class<? extends T> entityType;

    /**
     * C'tor
     */
    @SuppressWarnings("unchecked")
    public AbstractPurePersisterListener() {
        try {
            TypeVariable<Class<PurePersisterListener>> typeVariable = PurePersisterListener.class.getTypeParameters()[0];
            this.entityType =
                    (Class<T>) GenericTypeResolverImpl.getInstance().resolveTypeForFixedGenericType(typeVariable, this.getClass());
        } catch (IllegalArgumentException e) {
            throw new PersistenceException(PersistMessages.INVALID_BEAN_IMPLEMENTATION.create(this.getClass().getName()), e);
        }
    }

    /**
     * C'tor
     * 
     * @param entityType o tipo da entidade
     */
    public AbstractPurePersisterListener(Class<? extends T> entityType) {
        this.entityType = entityType;
    }

    /**
     * {@inheritDoc}
     */
    public void afterDelete(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public void afterInsert(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public void afterUpdate(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public void beforeDelete(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public void beforeInsert(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public void beforeUpdate(T target) {
    }

    /**
     * {@inheritDoc}
     */
    public final Class<? extends T> entityType() {
        return this.entityType;
    }

}
